package com.transaction.demo.trade.biz.impl;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.transaction.demo.common.util.JsonUtils;
import com.transaction.demo.common.util.ResultPage;
import com.transaction.demo.trade.biz.TradePaymentOrderBiz;
import com.transaction.demo.trade.dao.TradePaymentOrderDao;
import com.transaction.demo.trade.entity.TradePaymentOrderBean;
import com.transaction.demo.trade.enums.TradeStatusEnum;
import com.transaction.demo.trade.vo.req.InitPaymentOrderReqVo;
import com.transaction.demo.trade.vo.req.TradePaymentOrderListReqVo;
import com.transaction.demo.trade.vo.req.TradePaymentOrderReqVo;
import com.transaction.demo.trade.vo.req.TradePaymentOrderUpdateReqVo;
import com.transaction.demo.trade.vo.resp.TradePaymentOrderRespVo;

/**
 * 描述：支付订单表
 * 作者：马庆智
 * 日期：2023/04/27 17:22:10
 */
@Slf4j
@Service
public class TradePaymentOrderBizImpl implements TradePaymentOrderBiz {

	@Autowired
	private TradePaymentOrderDao tradePaymentOrderDao;

	/**
	 * 查询支付订单表
	 *
	 * @param tradePaymentOrder 请求参数
	 * @return TradePaymentOrderResp
	 * @author 马庆智
	 * @date: 2023/04/27 17:22:10
	 */
	@Override
	public TradePaymentOrderRespVo queryTradePaymentOrder(TradePaymentOrderReqVo tradePaymentOrder) {

		TradePaymentOrderBean query = tradePaymentOrderDao.queryTradePaymentOrder(
				TradePaymentOrderBean.builder().merchantOrderNo(tradePaymentOrder.getMerchantOrderNo()).build());

		if (query == null) {
			return null;
		}

		TradePaymentOrderRespVo orderRespVo = new TradePaymentOrderRespVo();
		BeanUtils.copyProperties(query, orderRespVo);
		return orderRespVo;
	}

	/**
	 * 查询支付订单表列表
	 *
	 * @param tradePaymentOrder 请求参数
	 * @return TradePaymentOrderResp
	 * @author 马庆智
	 * @date: 2023/04/27 17:22:10
	 */
	@Override
	public ResultPage<TradePaymentOrderRespVo> queryTradePaymentOrderList(
			TradePaymentOrderListReqVo tradePaymentOrder) {

		PageHelper.startPage(tradePaymentOrder.getCurrent(), tradePaymentOrder.getPageSize());
		List<TradePaymentOrderBean> query = tradePaymentOrderDao.queryTradePaymentOrderList(
				TradePaymentOrderBean.builder().merchantOrderNo(tradePaymentOrder.getMerchantOrderNo()).build());
		PageInfo<TradePaymentOrderBean> pageInfo = new PageInfo<>(query);

		List<TradePaymentOrderRespVo> list = pageInfo.getList().stream().map(x -> {
			TradePaymentOrderRespVo resp = new TradePaymentOrderRespVo();
			BeanUtils.copyProperties(x, resp);
			return resp;
		}).collect(Collectors.toList());

		return new ResultPage<>(list, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());

	}

	/**
	 * 添加支付订单表
	 *
	 * @param vo 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:22:10
	 */
	@Override
	public int saveTradePaymentOrder(InitPaymentOrderReqVo vo) {
		TradePaymentOrderBean orderBean = new TradePaymentOrderBean();
		orderBean.setId(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		orderBean.setVersion(1);
		orderBean.setCreateTime(new Date());
		orderBean.setEditor("koobe");
		orderBean.setCreater("koobe");
		orderBean.setEditTime(new Date());
		orderBean.setStatus(TradeStatusEnum.WAITING_PAYMENT.name());
		orderBean.setProductName("下单测试");
		orderBean.setMerchantOrderNo(vo.getBusinessId());
		orderBean.setOrderAmount("100");
		orderBean.setOrderFrom("a");
		orderBean.setMerchantName("a");
		orderBean.setMerchantNo(vo.getBusinessId());
		orderBean.setOrderTime(new Date());
		orderBean.setOrderDate(vo.getBusinessId());
		orderBean.setOrderIp("10.168.96.23");
		orderBean.setOrderRefererUrl("");
		orderBean.setReturnUrl("https://www.baidu.com");
		orderBean.setNotifyUrl("http://10.168.96.23:8100/order/v1/notify");
		orderBean.setCancelReason("123");
		orderBean.setOrderPeriod((short) 0);
		orderBean.setExpireTime(new Date());
		orderBean.setPayWayCode("123");
		orderBean.setPayWayName("123");
		orderBean.setRemark(vo.getRemark());
		orderBean.setTrxType("");
		orderBean.setTrxNo("123");
		orderBean.setPayTypeCode("123");
		orderBean.setPayTypeName("123");
		orderBean.setFundIntoType("");
		orderBean.setIsRefund("0");
		orderBean.setRefundTimes(0);
		orderBean.setSuccessRefundAmount("1");

		log.info("添加订单信息：{}", JsonUtils.toJson(orderBean));
		return tradePaymentOrderDao.saveTradePaymentOrder(orderBean);
	}

	/**
	 * 删除支付订单表
	 *
	 * @param ids 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:22:10
	 */
	@Override
	public int deleteTradePaymentOrder(String ids) {
		return tradePaymentOrderDao.deleteTradePaymentOrder(
				Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList()));
	}

	/**
	 * 更新支付订单表
	 *
	 * @param tradePaymentOrder 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:22:10
	 */
	@Override
	public int updateTradePaymentOrder(TradePaymentOrderUpdateReqVo tradePaymentOrder) {
		TradePaymentOrderBean bean = new TradePaymentOrderBean();
		BeanUtils.copyProperties(tradePaymentOrder, bean);
		return tradePaymentOrderDao.updateTradePaymentOrder(bean);
	}

}